package cn.toma.controller.sys;

import cn.toma.common.annotation.Log;
import cn.toma.common.exception.FfBootException;
import cn.toma.common.vo.StringPool;
import cn.toma.common.vo.router.VueRouter;
import cn.toma.config.web.utli.BaseController;
import cn.toma.manager.sys.UserManager;
import cn.toma.model.sys.Menu;
import cn.toma.service.sys.MenuService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Slf4j
@Validated
@RestController
@RequestMapping("/menu")
public class MenuController extends BaseController
{

	private String message;

	@Autowired
	private UserManager userManager;
	@Autowired
	private MenuService menuService;

	@GetMapping("/{username}")
	public List<VueRouter<Menu>>  getUserRouters(@NotBlank(message = "{required}") @PathVariable String username)
	{
		return this.userManager.getUserRouters(username);
	}

	@GetMapping
	@RequiresPermissions("menu:view")
	public Map<String, Object> menuList(Menu menu)
	{
		return this.menuService.findMenus(menu);
	}

	@Log("新增菜单/按钮")
	@PostMapping
	@RequiresPermissions("menu:add")
	public void addMenu(@Valid Menu menu) throws FfBootException
	{
		try {
			this.menuService.createMenu(menu);
		} catch (Exception e) {
			message = "新增菜单/按钮失败";
			log.error(message, e);
			throw new FfBootException(message);
		}
	}

	@Log("删除菜单/按钮")
	@DeleteMapping("/{menuIds}")
	@RequiresPermissions("menu:delete")
	public void deleteMenus(@NotBlank(message = "{required}") @PathVariable String menuIds) throws FfBootException
	{
		try {
			String[] ids = menuIds.split(StringPool.COMMA);
			this.menuService.deleteMeuns(ids);
		} catch (Exception e) {
			message = "删除菜单/按钮失败";
			log.error(message, e);
			throw new FfBootException(message);
		}
	}

	@Log("修改菜单/按钮")
	@PutMapping
	@RequiresPermissions("menu:update")
	public void updateMenu(@Valid Menu menu) throws FfBootException
	{
		try {
			this.menuService.updateMenu(menu);
		} catch (Exception e) {
			message = "修改菜单/按钮失败";
			log.error(message, e);
			throw new FfBootException(message);
		}
	}

//    @PostMapping("excel")
//    @RequiresPermissions("menu:export")
//    public void export(Menu menu, HttpServletResponse response) throws FfBootException {
//        try {
//            List<Menu> menus = this.menuService.findMenuList(menu);
//            ExcelKit.$Export(Menu.class, response).downXlsx(menus, false);
//        } catch (Exception e) {
//            message = "导出Excel失败";
//            log.error(message, e);
//            throw new FebsException(message);
//        }
//    }
}
